<?php
/**
* 
* ShopWindow Toolset
* 
* Copyright (C) 2008 Digital Window Ltd.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/


$aActiveRefineByGroups	= $oShopCore->oApiProductList->aActiveRefineByGroups;
$aRefineByGroups		= $oShopCore->oApiProductList->aRefineByGroups;


# Get previous refine by
$sRefineByQuery  = $oShopCore->createRefineByQuery($oShopCore->aRefineByActiveVals);
$sRefineByQuery .= !empty($sRefineByQuery) ? '|' : '';


# Active RB cancel links
$aCancelRefineByLinks = array();

if ( is_array($aActiveRefineByGroups) ) {
	foreach ($aActiveRefineByGroups as $key => $oRefineByGroup) {
		
		// passing zero unsets that definition
		$sRefineByCancelLink = $sRefineByQuery.$oRefineByGroup->iId."-0";
		
		$aCancelRefineByLinks[$oRefineByGroup->aRefineByDefinitions[0]->sName] = 
			$oShopCore->buildLink('', '', '', '', $oShopCore->sQuery, $sRefineByCancelLink);
	}
}
	

// add search Query as active RB
if ( strlen($oShopCore->sQuery) > 0 ) {
	// remove the appended comma
	if ( strlen($sRefineByQuery) > 0 ) {
		$sRefineByQueryStemmed = substr($sRefineByQuery, 0, -1);
	}
	
	$aCancelRefineByLinks[$oShopCore->sQuery] = $oShopCore->buildLink('', '', '', '', '', $sRefineByQueryStemmed);
}

// check not empty
if ( !empty($aRefineByGroups) ) {
	# Passed RB links
	foreach ($aRefineByGroups as $sGroupkey => &$oRefineByGroup) {
		
		$i = 0;
		
		foreach ($oRefineByGroup->aRefineByDefinitions as $key => $oRefineByDefinition) {
			
			// change first/last price range to user friendly text
			if ( $oRefineByGroup->iId == RB_PRICE ) {			
				if ( $i == 0 ) {
					$oRefineByDefinition->sName = 
						'up to '.substr_replace( $oRefineByDefinition->sName, '', 0, strpos($oRefineByDefinition->sName, '-')+1 );
				}
				elseif ( $i == count($oRefineByGroup->aRefineByDefinitions)-1 ) {
					$oRefineByDefinition->sName = 
						substr_replace( $oRefineByDefinition->sName, '', strpos($oRefineByDefinition->sName, '-')-1 ).' or more';
				}
			}
			// remove currently active category
			elseif ( $oRefineByGroup->iId == RB_CATEGORY && $oRefineByDefinition->iId == $oShopCore->oCategory->iId ) {
				unset($oRefineByGroup->aRefineByDefinitions[$key]);
			}
			
			// add the link property
			$sRefineByLink = $sRefineByQuery.$oRefineByGroup->iId."-".$oRefineByDefinition->sId;
				
			$oRefineByDefinition->sRefineByLink = $oShopCore->buildLink('', '', '', '', $oShopCore->sQuery, $sRefineByLink);
			
			$i++;
		}
		
		if ( ( $oRefineByGroup->iId == 1 && !REFINE_PRICE ) ||
			 ( $oRefineByGroup->iId == 2 && !REFINE_BRAND ) ||
			 ( $oRefineByGroup->iId == 3 && !REFINE_MERCHANT ) ||
			 ( $oRefineByGroup->iId == 4 && !REFINE_CATEGORY ) ) {
			unset($aRefineByGroups[$sGroupkey]);
		}
	}
}


# SMARTY ASSIGN
$oSmarty->assign('aRefineByGroups', $aRefineByGroups);
$oSmarty->assign('aCancelRefineByLinks', $aCancelRefineByLinks);

?>